package com.telecom.cms.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.telecom.cms.dbconnect.Chinese;
import com.telecom.cms.util.Constant;
import com.telecom.cms.dao.GetConnect;
import com.telecom.cms.dto.*;

/**
 * @author yueyingnishang
 * 
 */
public class CollectionAction extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 

		String foldername = Chinese.toChina(request.getParameter("foldername"));
		String isfolder = "no";

		int parfolderid = Integer.parseInt(request.getParameter("parfolderid"));

		// 新定义的主题应用插入到用户文件夹
		String kindid = (String) request.getSession().getAttribute("kindid");

		Connection conn = GetConnect.getCsConnection();

		conn.setAutoCommit(false);

		String insertsql1 = "insert into "
				+ Constant.SEG_USER_FOLDER_DEF
				+ "(FOLEDER_ID,PAR_FOLEDER_ID,FOLEDER_NAME,FOLEDER_DESC,SHARE_KIND,CREATE_TIME,USER_ID) "
				+ " values( ," + parfolderid + ",'" + foldername + "',"
				+ kindid + ",'',,'' ) ";
		PreparedStatement ps1 = conn.prepareStatement(insertsql1);

		ps1.executeUpdate();

		if (ps1 != null) {

			ps1.close();

		}
		// end
		if (isfolder.equals("isFolder")) {
			PreparedStatement psmt1 = conn
					.prepareStatement(" select FOLEDER_ID from "
							+ Constant.SEG_USER_FOLDER_DEF
							+ " where FOLEDER_NAME = '" + foldername + "'");
			ResultSet rs1 = psmt1.executeQuery();
			int folderid = 0;
			if (rs1.next()) {

				folderid = rs1.getInt(1);

			}
			if (rs1 != null) {

				rs1.close();
			}
			if (psmt1 != null) {

				psmt1.close();
			}//得到id
			insertsql1 = "insert into "
					+ Constant.SEG_USER_FOLDER_DEF
					+ "(FOLEDER_ID,PAR_FOLEDER_ID,FOLEDER_NAME,FOLEDER_DESC,SHARE_KIND,CREATE_TIME,USER_ID) "
					+ " values( ," + folderid + ",'"
					+ (" 以下为（" + foldername + "）内容") + "','2','',,'' ) ";
			ps1 = conn.prepareStatement(insertsql1);

			ps1.executeUpdate();

			if (ps1 != null) {

				ps1.close();

			}//插入子节点
			psmt1 = conn.prepareStatement(" select FOLEDER_ID from "
					+ Constant.SEG_USER_FOLDER_DEF + " where FOLEDER_NAME = '"
					+ (" 以下为（" + foldername + "）内容" + "'"));
			rs1 = psmt1.executeQuery();
			if (rs1.next()) {

				folderid = rs1.getInt(1);

			}
			if (rs1 != null) {

				rs1.close();
			}
			if (psmt1 != null) {

				psmt1.close();
			}

			insertsql1 = " insert into "
					+ Constant.SEG_APP_SUBJECT_DEF
					+ "(APP_SUBJECT_ID,APP_SUBJECT_NAME,APP_SUBJECT_DESC ,ANLS_CLASS_ID,FOLEDER_ID,JOINT_MODE_ID,USER_ID,CREATE_TIME, LATN_ID)"
					+ " values( ,'" + foldername + "','',''," + folderid
					+ ",'','',,0000)";

			ps1 = conn.prepareStatement(insertsql1);
			ps1.executeUpdate();
			if (ps1 != null) {

				ps1.close();

			}
			return mapping.findForward("success");
		}
		// 应用主题表插入记录

		// 先取文件夹表的文件ID
		PreparedStatement psmt1 = conn
				.prepareStatement(" select FOLEDER_ID from "
						+ Constant.SEG_USER_FOLDER_DEF
						+ " where FOLEDER_NAME = '" + foldername + "'");
		ResultSet rs1 = psmt1.executeQuery();
		int folderid = 0;
		if (rs1.next()) {

			folderid = rs1.getInt(1);

		}
		if (rs1 != null) {

			rs1.close();
		}
		if (psmt1 != null) {

			psmt1.close();
		}

		String insertsql2 = " insert into "
				+ Constant.SEG_APP_SUBJECT_DEF
				+ "(APP_SUBJECT_ID,APP_SUBJECT_NAME,APP_SUBJECT_DESC ,ANLS_CLASS_ID,FOLEDER_ID,JOINT_MODE_ID,USER_ID,CREATE_TIME, LATN_ID)"
				+ " values( ,'" + foldername + "','',''," + folderid
				+ ",'','',,0000)";

		PreparedStatement ps2 = conn.prepareStatement(insertsql2);
		ps2.executeUpdate();
		if (ps2 != null) {

			ps2.close();

		}
		// end
		// 分群规则定义表表插入
		// 先取主题表的主题ID
		PreparedStatement psmt2 = conn
				.prepareStatement(" select APP_SUBJECT_ID from "
						+ Constant.SEG_APP_SUBJECT_DEF + " where FOLEDER_ID = "
						+ folderid);
		ResultSet rs2 = psmt2.executeQuery();
		int APP_SUBJECT_ID = 0;
		if (rs2.next()) {

			APP_SUBJECT_ID = rs2.getInt(1);

		}
		if (rs2 != null) {

			rs2.close();
		}
		if (psmt2 != null) {

			psmt2.close();
		}

		List atomlist = (List) request.getSession().getAttribute("AtomList");

		PreparedStatement psmt3 = null;

		for (int i = 0; i < atomlist.size(); i++) {

			ShowatomDTO dto = (ShowatomDTO) atomlist.get(i);

			String insertsql3 = "insert into "
					+ Constant.SEG_SEG_RULE_DEF
					+ "(RULE_ID,RULE_KIND_ID,APP_SUBJECT_ID, BIZ_ATOM_ID, BIZ_ATOM_NAME,VAULE_ONE,VALUE_TWO,VALUE_TYPE_ID,RST_CDN_ID,RST_CDN_NAME,RST_CDN_TYPE)"
					+ " values( , ," + APP_SUBJECT_ID + "," + dto.getAtomid()
					+ ",'" + dto.getAtomname() + "','"
					+ dto.getAtomvalue().replaceAll("'", "''") + "','"
					+ dto.getChinesevalue().replaceAll("'", "''")
					+ "','','','','')";

			psmt3 = conn.prepareStatement(insertsql3);

			psmt3.executeUpdate();
		}

		if (psmt3 != null) {

			psmt3.close();
		}

		// end
		// 分群规则展示结果定义表插入
		List outputlist = (List) request.getSession()
				.getAttribute("Outputlist");
		PreparedStatement psmt4 = null;

		if (outputlist != null) {

			for (int j = 0; j < outputlist.size(); j++) {

				SEG_BIZ_ATOM_DEFDTO dto = (SEG_BIZ_ATOM_DEFDTO) outputlist
						.get(j);

				String insertsql4 = " insert into "
						+ Constant.SEG_SEG_RULE_SHOWATOM
						+ "(BIZ_ATOM_ID,APP_SUBJECT_ID, BIZ_ATOM_NAME, RULE_KIND_ID) values("
						+ dto.getBIZ_ATOM_ID() + "," + APP_SUBJECT_ID + ",'"
						+ dto.getBIZ_ATOM_NAME() + "',)";
				psmt4 = conn.prepareStatement(insertsql4);

				psmt4.executeUpdate();

			}

			if (psmt4 != null) {

				psmt4.close();
			}
		}
		// end
		try {
			conn.commit();

		} catch (Exception e) {

			conn.rollback();

		} finally {

			conn.setAutoCommit(true);

		}

		return mapping.findForward("success");
	}

}
